package com.jiedeng.controller;

import com.jiedeng.dao.INormRepository;
import com.jiedeng.model.Norm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 覃远龙
 * @ClassName: NormController
 * @Description:
 * @date 2017/8/9 15:25
 */
@RestController
@RequestMapping("/norm")
public class NormController {

    @Autowired
    INormRepository normRepository;

    //----------------- norm table -------------------//
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public List<Norm> searchNorm(int normType, int normGrade){
        if(normType > 0 && normGrade <= 0){
            return normRepository.findByNormTypeId(normType);
        }else if(normType <= 0 && normGrade > 0){
            return normRepository.findByNormGradeId(normGrade);
        }else{
            return normRepository.findAll();
        }
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Norm> findAllNorm(){
        return normRepository.findAll();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Norm findNorm(long id){
        return normRepository.getOne(id);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Norm saveNorm(@RequestBody Norm norm){
        return normRepository.save(norm);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public void deleteNorm(long id){
        normRepository.deleteById(id);
    }


}
